package com.heima.behavior.service.impl;

import com.heima.behavior.service.*;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    ApLikesBehaviorService apLikesBehaviorService;
    @Autowired
    ApUnlikeBehaviorService apUnlikeBehaviorService;
    @Autowired
    ApCollectionBehaviorService apCollectionBehaviorService;

    /**
     * 加载文章详情 数据回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
//        1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();

        if (user == null) {
            // 2. 未登录 直接将 4种行为 均返回false
            Map<String, Boolean> data = new HashMap<>();
            data.put("isfollow", false);
            data.put("islike", false);
            data.put("isunlike", false);
            data.put("iscollection", false);
            return ResponseResult.okResult(data);
        }
        //3. 如果登录,查询行为实体
//        Query query = Query.query(Criteria.where("refId").is(user.getId()));
//        ApBehaviorEntry behaviorEntry = mongoTemplate.findOne(query, ApBehaviorEntry.class);
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (behaviorEntry == null) {
            ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            return getResponseResult(dto, user, entry);
        }
//        5. 根据行为实体 文章ID查询 是否点赞
        return getResponseResult(dto, user, behaviorEntry);
    }

    private ResponseResult getResponseResult(ArticleBehaviorDTO dto, ApUser user, ApBehaviorEntry behaviorEntry) {
        Query query2 = Query.query(Criteria.where("articleId").is(dto.getArticleId()).
                and("entryId").is(behaviorEntry.getId()));
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query2, ApLikesBehavior.class);
        Map<String, Boolean> data = new HashMap<>();
        if (likesBehavior == null) {
            data.put("islike", false);

        } else {
            data.put("islike", true);
        }
//        6. 根据行为实体 文章ID查询 是否不喜欢
        Query query3 = Query.query(Criteria.where("articleId").is(dto.getArticleId()).
                and("entryId").is(behaviorEntry.getId()));
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query3, ApUnlikesBehavior.class);
        if (unlikesBehavior == null) {
            data.put("isunlike", false);
        } else {
            data.put("isunlike", true);
        }
//        7. 根据行为实体 文章ID查询 是否收藏
        Query query4 = Query.query(Criteria.where("articleId").is(dto.getArticleId()).
                and("entryId").is(behaviorEntry.getId()));
        ApCollection apCollection = mongoTemplate.findOne(query4, ApCollection.class);
        if (apCollection == null) {
            data.put("iscollection", false);
        } else {
            data.put("iscollection", true);
        }
//        8. 根据登录用户id 去redis中查询是否关注该作者
        Integer authorApUserId = dto.getAuthorApUserId();
        if (authorApUserId != null) {
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(authorApUserId));
            if (score == null) {
                data.put("isfollow", false);
            } else {
                data.put("isfollow", true);
            }
        }
//        9. 封装结果 返回
        return ResponseResult.okResult(data);
    }
}
